package com.aidong.service;

/**
 * Created by john on 16-11-2.
 */

import com.aidong.cache.ItemCache;
import com.aidong.common.JobConsts;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.PayListDao;
import com.aidong.dao.wills.ReportSalesByCashierIDDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.ReportCashier;
import com.aidong.util.DateUtil;
import com.aidong.util.ExportUtil;
import com.aidong.util.FileCheckUtil;
import com.aidong.util.ReportUtil;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

@Service
public class CashierReportService extends BaseJobService {
    protected Logger log = Logger.getLogger(CashierReportService.class);
    @Resource
    ReportSalesByCashierIDDao reportSalesByCashierIDDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;
    @Resource
    ReportUtil reportUtil;
    @Resource
    protected ItemCache cache;
    @Resource
    PayListDao payListDao;

    public CashierReportService() {
    }

    public synchronized void doJob() {
        Thread th1 = new Thread() {
            public void run() {
                try {
                    try {
                        Date e = new Date(System.currentTimeMillis() - 86400000L);
                        CashierReportService.this.reportSalesByCashierIDDao.doCashierReport(e, e);
                        CashierReportService.this.doCashierReport();
                    } catch (Exception var5) {
                        var5.printStackTrace();
                    }

                } finally {
                    ;
                }
            }
        };
        th1.start();
    }

    private void doCashierReport() throws Exception {
        try {
            Timestamp e = DateUtil.getCurrentTimestamp();
            String cur = DateUtil.formatDate(e, DateUtil.DATE_PATTERN_YYYYMMDD);
            String curDate = DateUtil.addDays(cur, -1);
            HashMap commonData = new HashMap();
            commonData.put("date", curDate);
            Map data = this.createAllData(false, "");
            data.putAll(commonData);
            String fileEnName = curDate + "_" + JobConsts.TEMPLATE_NAME_CASHIER_REPORT + "(ALL)" + JobConsts.EXCEL_SUFFIX;
            String fileChName = curDate + "_" + JobConsts.CH_NAME_CASHIER_REPORT + "(总店)" + JobConsts.EXCEL_SUFFIX;
            String filePath = this.getDownloadPath() + curDate + "\\" + JobConsts.TEMPLATE_NAME_CASHIER_REPORT;
            String fileDbPath = curDate + "\\" + JobConsts.TEMPLATE_NAME_CASHIER_REPORT;
            FileCheckUtil.checkFilePath(filePath);
            ExportUtil.exportExcel(filePath, fileEnName, this.getTemplatePath(), JobConsts.TEMPLATE_NAME_CASHIER_REPORT + JobConsts.EXCEL_SUFFIX, data);
            this.reportUtil.addReportFile("02", fileEnName, fileChName, fileDbPath, curDate, "ALL");
            List shopLst = this.dicShopCodeDao.getAllShopList();
            Iterator i$ = shopLst.iterator();

            while (i$.hasNext()) {
                DicShopCode shop = (DicShopCode) i$.next();
                Map shopData = this.createShopData(shop.getCode(), false, "");
                shopData.putAll(commonData);
                fileEnName = curDate + "_" + JobConsts.TEMPLATE_NAME_CASHIER_REPORT + "(" + shop.getCode() + ")" + JobConsts.EXCEL_SUFFIX;
                fileChName = curDate + "_" + JobConsts.CH_NAME_CASHIER_REPORT + "(" + shop.getChName() + ")" + JobConsts.EXCEL_SUFFIX;
                ExportUtil.exportExcel(filePath, fileEnName, this.getTemplatePath(), JobConsts.TEMPLATE_NAME_CASHIER_REPORT + JobConsts.EXCEL_SUFFIX, shopData);
                this.reportUtil.addReportFile("02", fileEnName, fileChName, fileDbPath, curDate, shop.getCode());
            }
        } catch (Exception var14) {
            var14.printStackTrace();
            this.log.error("生成CashierReport失败");
            return;
        }

        this.log.info("生成CashierReport成功");
    }

    public void createCashierReport(String createts) throws Exception {
        try {
            Date e = DateUtil.formatDate(createts);
            String cur = DateUtil.formatDate(e, DateUtil.DATE_PATTERN_YYYYMMDD);
            String curDate = createts;
            HashMap commonData = new HashMap();
            commonData.put("date", createts);
            Map data = this.createAllData(true, createts);
            data.putAll(commonData);
            String fileEnName = createts + "_" + JobConsts.TEMPLATE_NAME_CASHIER_REPORT + "(ALL)" + JobConsts.EXCEL_SUFFIX;
            String fileChName = createts + "_" + JobConsts.CH_NAME_CASHIER_REPORT + "(总店)" + JobConsts.EXCEL_SUFFIX;
            String filePath = this.getDownloadPath() + createts + "\\" + JobConsts.TEMPLATE_NAME_CASHIER_REPORT;
            String fileDbPath = createts + "\\" + JobConsts.TEMPLATE_NAME_CASHIER_REPORT;
            FileCheckUtil.checkFilePath(filePath);
            ExportUtil.exportExcel(filePath, fileEnName, this.getTemplatePath(), JobConsts.TEMPLATE_NAME_CASHIER_REPORT + JobConsts.EXCEL_SUFFIX, data);
            this.reportUtil.addReportFile("02", fileEnName, fileChName, fileDbPath, createts, "ALL");
            List shopLst = this.dicShopCodeDao.getAllShopList();
            Iterator i$ = shopLst.iterator();

            while (i$.hasNext()) {
                DicShopCode shop = (DicShopCode) i$.next();
                Map shopData = this.createShopData(shop.getCode(), true, createts);
                shopData.putAll(commonData);
                fileEnName = curDate + "_" + JobConsts.TEMPLATE_NAME_CASHIER_REPORT + "(" + shop.getCode() + ")" + JobConsts.EXCEL_SUFFIX;
                fileChName = curDate + "_" + JobConsts.CH_NAME_CASHIER_REPORT + "(" + shop.getChName() + ")" + JobConsts.EXCEL_SUFFIX;
                ExportUtil.exportExcel(filePath, fileEnName, this.getTemplatePath(), JobConsts.TEMPLATE_NAME_CASHIER_REPORT + JobConsts.EXCEL_SUFFIX, shopData);
                this.reportUtil.addReportFile("02", fileEnName, fileChName, fileDbPath, curDate, shop.getCode());
            }
        } catch (Exception var15) {
            var15.printStackTrace();
            this.log.error("生成CashierReport失败");
            return;
        }

        this.log.info("生成CashierReport成功");
    }

    private Map<String, Object> createAllData(boolean flg, String serDate) {
        HashMap data = new HashMap();
        List allLst;
        if (flg) {
            allLst = this.reportSalesByCashierIDDao.getCashierDataAllByDay(serDate);
        } else {
            allLst = this.reportSalesByCashierIDDao.getCashierDataAll();
        }

        ArrayList lst = new ArrayList();
        ArrayList cashier = new ArrayList();
        double allAmount = 0.0D;
        String preShopCode = "";
        String preCashierName = "";

        ReportCashier po;
        for (Iterator i$ = allLst.iterator(); i$.hasNext(); allAmount += po.getAmount().doubleValue()) {
            po = (ReportCashier) i$.next();
            if (!po.getShopCode().equals(preShopCode) || !po.getCashierName().equals(preCashierName)) {
                if (cashier.size() == 0 && !"".equals(preShopCode)) {
                    cashier.add(new ReportCashier());
                }

                if (cashier.size() > 0) {
                    lst.add(cashier);
                }

                cashier = new ArrayList();
                preShopCode = po.getShopCode();
                preCashierName = po.getCashierName();
            }

            cashier.add(po);
        }

        if (cashier.size() > 0) {
            lst.add(cashier);
        }

        if (allLst.size() == 0) {
            cashier.add(new ReportCashier());
            lst.add(cashier);
        }

        data.put("cashierList", lst);
        data.put("allAmount", Double.valueOf(allAmount));
        return data;
    }

    private Map<String, Object> createShopData(String shopCode, boolean flg, String serDate) {
        HashMap data = new HashMap();
        List allLst;
        if (flg) {
            allLst = this.reportSalesByCashierIDDao.getCashierDataByShopCodeAndDay(shopCode, serDate);
        } else {
            allLst = this.reportSalesByCashierIDDao.getCashierDataByShopCode(shopCode);
        }

        ArrayList lst = new ArrayList();
        ArrayList cashier = new ArrayList();
        double allAmount = 0.0D;
        String preCashierName = "";

        ReportCashier po;
        for (Iterator i$ = allLst.iterator(); i$.hasNext(); allAmount += po.getAmount().doubleValue()) {
            po = (ReportCashier) i$.next();
            if (!po.getCashierName().equals(preCashierName)) {
                if (cashier.size() == 0 && !"".equals(preCashierName)) {
                    cashier.add(new ReportCashier());
                }

                if (cashier.size() > 0) {
                    lst.add(cashier);
                }

                cashier = new ArrayList();
                preCashierName = po.getCashierName();
            }

            cashier.add(po);
        }

        if (cashier.size() > 0) {
            lst.add(cashier);
        }

        if (allLst.size() == 0) {
            cashier.add(new ReportCashier());
            lst.add(cashier);
        }

        data.put("cashierList", lst);
        data.put("allAmount", Double.valueOf(allAmount));
        return data;
    }
}
